package com.athuahua.spzx.task;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.athuahua.spzx.entity.OrderStatistics;
import com.athuahua.spzx.mapper.OrderInfoMapper;
import com.athuahua.spzx.mapper.OrderStatisticsMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: huahua
 * @Date: 2024/8/6 0006
 * @Time: 14:18
 * @Description:
 */
@Component
@Slf4j
public class OrderTask {
    //定时任务
    @Resource
    private OrderInfoMapper orderInfoMapper;

    @Resource
    private OrderStatisticsMapper orderStatisticsMapper;


    //每天凌晨2点查询前一天的统计数据，把统计的数据添加到统计的结果表中
    @Scheduled(cron = "0 0 2 * * ?")
    public void orderTotalAmountStatistics() {
        //1 获取前一天的日期
        String yesterday = DateUtil.offsetDay(new Date(), -1).toString("yyyy-MM-dd");

        //2 根据前一天的日期进行的统计功能
        OrderStatistics orderStatistics =
                orderInfoMapper.selectStatisticsByDate(yesterday);

        //3 统计之后的数据添加到统计结果表中
        if(orderStatistics != null){
            orderStatistics.setId(null); //先把id滞空，避免主键冲突
            orderStatistics.setOrderDate(DateUtil.offsetDay(new Date(), -1));
            orderStatisticsMapper.insert(orderStatistics);
        }



    }
}
